<?php

class ReviewController extends BaseController {

    public function index($iCourse = null) {

        $iCourse = $iCourse ? $iCourse : CNamHoa::CurrentCourse();

        return View::make("review.page", [
                'sPage_Title'       => trans('navigation.review'),
                'sPage_Description' => trans('navigation.review-description'),
                'iCourse'           => $iCourse,
                'Options'           => Cache::get('options'),
                'aClasses'          => Classes::where('course', $iCourse)->get(),
        ]);
    }

    public function postLoadUser() {

        $Classes          = Classes::find(Input::get('class_id'));
        $aUser            = $Classes ? $Classes->user()->thieunhi()->get() : [];
        $aUserCore        = [];
        $aChuyenCanHeader = [];
        $aUserChuyenCan   = [];
        $aHocLucHeader    = [];
        $aUserHocLuc      = [];
        $Option           = Cache::get('options');

        if ($aUser->count()) {
            $iCourse = $Classes->course;

            $Items = UserScore::chuyencan([
                    'start' => $Option['COURSE'][$iCourse]->data->start,
                    'end'   => $Option['COURSE'][$iCourse]->data->end,
                    ], $aUser->lists('id'))
                ->get();

            foreach ($Items as $Info) {
                $aChuyenCanHeader[$Info->date]                                 = $Info->date;
                $aUserChuyenCan[$Info->user_id][$Info->date][$Info->scoretype] = $Info->score;
            }

            $Items = UserScore::hocluc($aUser->lists('id'), NULL, NULL, $iCourse)->get();

            foreach ($Items as $Info) {
                $aHocLucHeader[$Info->scoretype]                            = $Info->scoretype;
                $aUserHocLuc[$Info->user_id][$Info->scoretype][$Info->data] = $Info->score;
            }
        }

        return View::make("review.table", [
                'Option'           => $Option,
                'Classes'          => $Classes,
                'aUser'            => $aUser,
                'aUserCore'        => $aUserCore,
                'aChuyenCanHeader' => $aChuyenCanHeader,
                'aHocLucHeader'    => $aHocLucHeader,
                'aUserChuyenCan'   => $aUserChuyenCan,
                'aUserHocLuc'      => $aUserHocLuc,
        ]);
    }

    public function postAjaxForm() {
        $User    = User::find(Input::get('user_id'));
        $Classes = $User->classes()->where('class_id', Input::get('class_id'))->first();

        return View::make("review.modal", [
                'User'    => $User,
                'Classes' => $Classes,
                'Option'  => Cache::get('options'),
                'pivot'   => $Classes->pivot,
        ]);
    }

    public function postAjaxUpdate() {

        $pivot  = User::find(Input::get('user_id'))->classes()->where('class_id', Input::get('class_id'))->first()->pivot;
        $Option = Cache::get('options');

        if ($pivot->rank != Input::get('rank') || $pivot->note != Input::get('note')) {
            $pivot->rank    = Input::get('rank');
            $pivot->note    = Input::get('note');
            $pivot->updater = Auth::user()->id;
            $pivot->save();
        }

        $aResponse = array(
            'status'  => 200,
            'user_id' => Input::get('user_id'),
            'rank'    => isset($Option['RANK'][$pivot->rank]) ? $Option['RANK'][$pivot->rank]->data : $pivot->rank,
        );

        return Response::json($aResponse);
    }

}
